package com.zyh.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.zyh.entity.*;
import com.zyh.service.IAdminRoleService;
import com.zyh.service.IResourceService;
import com.zyh.service.IRoleResourceService;
import com.zyh.service.IRoleService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhang
 * @since 2022-05-08
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IAdminRoleService adminRoleService;
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IRoleResourceService roleResourceService;
    //给用户赋角色
    @PostMapping("/grantRidForUserId")
    public ResultEntity grantRidForUserId(@RequestBody AdminRoleDto adminRoleDto){
        Integer userId = adminRoleDto.getUserId();
        Integer[] ids = adminRoleDto.getRoleIds();
        for(int i = 0;i<ids.length;i++){
            UpdateWrapper<AdminRole> wrapper = new UpdateWrapper<AdminRole>()
                    .eq(true,"admin_id",userId).eq(true,"role_id",ids[i]);
            AdminRole adminRole = new AdminRole();
            adminRole.setAdminId(adminRoleDto.getUserId());
            adminRole.setRoleId(ids[i]);
            adminRoleService.saveOrUpdate(adminRole, wrapper);
        }
        return ResultEntity.ok();
    }
    //角色列表展示
    @GetMapping("/selectAll")
    public ResultEntity<List<Role>> selectAll(){
        return ResultEntity.ok(roleService.list());
    }
    //添加角色
    @PostMapping("/add")
    public ResultEntity<Role> add(@RequestBody Role role){
        return roleService.save(role)==true ? ResultEntity.ok() : ResultEntity.error("添加失败！");
    }
    //删除角色
    @DeleteMapping("/delete/{roleId}")
    public ResultEntity<Role> delete(@PathVariable("roleId") Integer roleId){
        return roleService.removeById(roleId)==true ? ResultEntity.ok() : ResultEntity.error("删除失败！");
    }
    //根据角色标记查询角色信息
    @GetMapping("/selectOne/{roleId}")
    public ResultEntity<Role> selectOne(@PathVariable("roleId") Integer roleId){
        return ResultEntity.ok(roleService.getById(roleId));
    }
    //给角色赋权
    @PostMapping("/grantResource")
    public ResultEntity grantResource(@RequestBody RoleResourceDto roleResourceDto){
        Integer roleId = roleResourceDto.getRoleId();
        Integer[] resourceIds = roleResourceDto.getResourceIds();
        for (int i = 0; i < resourceIds.length; i++) {
            UpdateWrapper<RoleResource> wrapper = new UpdateWrapper<RoleResource>();
            wrapper.eq(true,"role_id",roleId).eq(true,"resource_id",resourceIds[i]);
            RoleResource roleResource = new RoleResource();
            roleResource.setRoleId(roleId);
            roleResource.setResourceId(resourceIds[i]);
            roleResourceService.saveOrUpdate(roleResource,wrapper);
        }
        return ResultEntity.ok();
    }

    //获取某个角色的权限Id
    @GetMapping("/selectResourcesByRoleId/{roleId}")
    public ResultEntity<List<RoleResource>> selectResourcesByRoleId(@PathVariable("roleId") Integer roleId){
        return ResultEntity.ok(roleResourceService.list(new QueryWrapper<RoleResource>().eq("role_id",roleId)));
    }

    //修改角色信息
    @PutMapping("/update")
    public ResultEntity update(@RequestBody Role role){
        return roleService.updateById(role)==true ? ResultEntity.ok() : ResultEntity.error();
    }
}

